package dom4j;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.Iterator;

public class test {

    public static void main(String[] args) {
        //创建SAXReader对象，用于读取xml文件
        SAXReader reader = new SAXReader();
        Document doc = null;

        {
            try {
                //读取xml文件获得Document对象
                doc = reader.read(new File("F:\\IDEACodeOFJavaEE\\XMLStudy\\src\\dom4j\\phoneInfo.xml"));
               //获取根元素
                Element root = doc.getRootElement();
                System.out.println(root.getName());
                //获取根元素的子元素，通过迭代器
              Iterator<Element> it  = root.elementIterator();
              while (it.hasNext()){
               Element element = it.next();
              Attribute ai = element.attribute("name");
                  System.out.println(element.getName()+":"+ai.getValue());
                  Iterator<Element> cit =  element.elementIterator();
                  while (cit.hasNext()){
                      Element  et  =cit.next();

                          Element type = et.element("Type");
                      if(et!=null || type != null) {
                          System.out.println(et.getName() + ":" + type.getText());
                      }

                  }
              }
            } catch (DocumentException e) {
                e.printStackTrace();
            }
        }
    }

}
